//
//  ClassifyCollectionViewCell.m
//  TodatPlay
//
//  Created by dllo on 16/9/10.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ClassifyCollectionViewCell.h"
#import "HotCollectionViewCell.h"
#import "TAMacro.h"
#import "CDMNetTool.h"
#import "FeaturedThreeData.h"
#import "FeaturedThreeRootClass.h"
#import "UIImageView+WebCache.h"
#import "DetailsViewController.h"
#import "SelfSizeTool.h"
#import "ClassifyViewController.h"

#define kCollectionViewCellReuse @"hotCellReuse"
#define kHotDataForRow self.rootClassOfThree.data[indexPath.row]
@interface ClassifyCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *myCollecyionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *myLayout;

@property (nonatomic, strong) FeaturedThreeRootClass *rootClassOfThree;
@end

@implementation ClassifyCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self configCollectionView];
    [self handleData];
}
- (void)configCollectionView {
    [self.myCollecyionView registerNib:[UINib nibWithNibName:@"HotCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionViewCellReuse];
    self.myCollecyionView.dataSource = self;
    self.myCollecyionView.delegate = self;
    self.myCollecyionView.bounces = NO;
    self.myLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.myLayout.minimumInteritemSpacing = 0;
    self.myLayout.minimumLineSpacing = 0;
    
}
- (void)setCateid:(NSString *)cateid {
    _cateid = cateid;
    [self.myCollecyionView reloadData];
    [self handleData];
}
- (void)handleData {
    NSString *HTTPStr = @"http://wanapi.damai.cn/projlistnew.json?platform=4&order=pri&source=10345&psize=10&cityid=110000&version=203004&channel_from=m360_market&pindex=0";
    NSString *cateidStr = [NSString stringWithFormat:@"&cateid=%@", _cateid];
    
    NSString *finalStr = [HTTPStr stringByAppendingPathComponent:cateidStr];
    [CDMNetTool GET:finalStr andBody:nil andHeader:nil andResponse:CDMJSON andSuccessBlock:^(id result) {
        self.rootClassOfThree = [FeaturedThreeRootClass modelWithDic:result];
        [self.myCollecyionView reloadData];
//        NSLog(@"请求成功%@", result);
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败%@", error);
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.rootClassOfThree.data.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    NSURL *picURL = [NSURL URLWithString:self.rootClassOfThree.data[indexPath.row].pic];
    NSURL *orgavatarURL = [NSURL URLWithString:self.rootClassOfThree.data[indexPath.row].orgavatar];
    
    [cell.orgavatarImageView sd_setImageWithURL:orgavatarURL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    }];
    [cell.picImageView sd_setImageWithURL:picURL placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
    /***************** 处理字符串数据(分割添加) ******************/
    NSString *pricestrlabel = kHotDataForRow.pricestr;
    if ([pricestrlabel containsString:@"-"]) {
        NSArray *arr = [pricestrlabel componentsSeparatedByString:@"-"];
        pricestrlabel = nil;
        pricestrlabel = [NSString stringWithFormat:@"%@起", arr.firstObject];
    } else if ([pricestrlabel containsString:@"元"]) {
        NSArray *arr = [pricestrlabel componentsSeparatedByString:@"元"];
        pricestrlabel = [NSString stringWithFormat:@"%@起", arr.firstObject];
    }
    // 处理字符串
    NSString *discountStr = kHotDataForRow.discount;
    if ([discountStr containsString:@"-"]) {
        NSArray *arr = [pricestrlabel componentsSeparatedByString:@"-"];
        discountStr = nil;
        discountStr = [NSString stringWithFormat:@"%@元", arr.firstObject];
    }
    // label字体加横线
    NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", discountStr]];
    [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
    
    /***************** 处理结束 ******************/
    cell.highlightsLabel.text = kHotDataForRow.highlights;
    cell.pricestrLabel.text = pricestrlabel;
    cell.discountLabel.attributedText = newPrice;
    cell.titleLabel.text = kHotDataForRow.title;
    cell.timestrLabel.text = kHotDataForRow.timestr;
    cell.addressLabel.text = kHotDataForRow.address;
    
    /**================= 跳转方法 =====================*/
    [cell cell:^(UITapGestureRecognizer *cellImageTap) {

        DetailsViewController *detailsVC = [[DetailsViewController alloc] init];
        
        detailsVC.parm = [NSString stringWithFormat:@"%@", self.rootClassOfThree.data[indexPath.row].ID];
        id responder = self.nextResponder;
        while (![responder isKindOfClass:[UINavigationController class]] && responder != nil) {
            responder = [responder nextResponder];
        }

        ClassifyViewController *rootViewController = (ClassifyViewController *)responder;
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:detailsVC];
        navi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

        [rootViewController presentViewController:navi animated:YES completion:nil];
        
    }];
    return cell;

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat addHeight = [SelfSizeTool selfSizeHeightWithString:kHotDataForRow.title width:SCREEN_WIDTH font:[UIFont systemFontOfSize:15]];
    return CGSizeMake(SCREEN_WIDTH, 350 + addHeight + 0.5);
}

@end
